<?php
class CouponAction extends CommonAction {
	
	/**
	 * 删除
	 * @param unknown_type $id
	 */
	public function del() {
//		$result = false;
// 		$coupon = M ( "Coupon" );
// 		$result = $coupon->delete($id);

// 		$order = D ("Order");
// 		$total = 0;
		
// 		$result = $this->deleteById($id);
// 		if ($result == false) {
// 			echo json_encode (array(
// 					'errorMsg' => '删除出错！' 
// 			));
// 		} else {
// 			echo json_encode (array(
// 					'success' => true 
// 			));
// 		}
		
		$result = false;
		$Coupon = D ( "Coupon" );
		
		$ids = $_POST['ids'];
		$data["status"]  = -1; //表明已删除
		$arrayIds = explode(',',$ids);
		
		$yhk_names = '';
		$Coupon->startTrans();
		foreach ($arrayIds as $id){
			$yhk_bh = $Coupon->where('Id='.$id)->field('YHK_BH,YHK_MC')->select(); //获取优惠卡名称
				
			$order = D ("Order");
			$total = 0;
			$total = $order->where("DD_SYYHQ = '".$yhk_bh[0]['YHK_BH']."'")->count (); // 计算总数
			if($total == 0){
				$result = $Coupon->where('Id='.$id)->delete();
			}else{
				$yhk_names = $yhk_names.$yhk_bh[0]['YHK_MC'].",";
			}
		}
		
		if($result){
			$Coupon->commit();
		}else{
			$Coupon->rollback();
		}
		$yhk_names = substr($yhk_names,0,strlen($yhk_names)-1);
		$errmsg = "优惠卡    ".$yhk_names." 目前存在报名中，无法删除!";
		
		if ($result == false) {
			echo json_encode (array(
					'errorMsg' => $errmsg
			));
		} else {
			echo json_encode (array(
					'success' => true
			));
		}
	}
	
	/**
	 * 修改
	 * @param 
	 */
	public function save() {
		$result = false;
		$id = $_GET["id"];
		$coupon = M ("Coupon");
		if($coupon->create()) {
			$result = $coupon->where('id='.$_REQUEST['id'] )->save();
			if($result) {
				echo json_encode (array('success'=>true));
			}else{
				echo json_encode (array('errorMsg'=>'修改出错！'));
			}
		}else{
			echo json_encode (array('errorMsg'=>$coupon->getError()));
		}
	}
	
	/**
	 * 新增
	 */
	public function add() {
		$xqid =  $_POST["ZM_XQ"];
		if(empty($xqid)){
			if(isset($_SESSION['xqid'])) {
				$xqid =$_SESSION['xqid'];
			}
		}

// 		$object = array();
		$result = false;
// 		$coupon = M("Coupon");
		$couponMode= D('Coupon');
		$_POST["YHK_BH"] = $couponMode->getCouponLSBH($xqid,date("Ymd"));
		$_POST["YHK_CJRID"] = $_SESSION['loginUserId'];
		$_POST["YHK_CJRMC"] = $_SESSION['loginUserName'];
		if($couponMode->create()) {
			$result = $couponMode->add();
			if($result) {
				echo json_encode (array('success'=>true));
			}else{
				echo json_encode (array('errorMsg'=>'添加出错！'));
			}
		}else{
			echo json_encode (array('errorMsg'=>$couponMode->getError()));
		}

	}
	
	/**
	 * 查询(non-PHPdoc)
	 * @see CommonAction::read()
	 */
	public function read() {
		$map =  array ();
		
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$coupon = M ("Coupon");
		$total = $coupon->where($map)->count (); // 计算总数	

		$accountList = $coupon->where($map)->limit (($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
		$resultJson = json_encode($accountList);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中		
		echo $json;
	}
	
	public function getDisplayList(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id';
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
		$offset = ($page-1)*$rows;
		
// 		$xqid ='';
// 		if(isset($_SESSION['xqid'])) {
// 			$xqid =$_SESSION['xqid'];
// 		}
// 		if(!empty($xqid)){
// 			$xqid =  $_POST["XQ"];
// 		}

		$xqid =  $_POST["XQ"];
		if(empty($xqid)){
			$xqid =  $_SESSION['XqFilterString'];
		}else{
			$xqid = "'" . $xqid ."'";
		}
		
		$LX = $_POST['LX'];
		$YHKMC = $_POST['YHKMC'];
		$CJR= $_POST['CJR'];
		$JE_GREATER = $_POST['JE_GREATER'];
		$JE_LESS = $_POST['JE_LESS'];
		$KSSJ_START = $_POST['KSSJ_START'];
		$JSSJ_END = $_POST['JSSJ_END'];
		
		$coupon = D ( "Coupon" );
		$result = $coupon->getModeDisplayList($xqid,$LX,$CJR,$YHKMC,$JE_GREATER,$JE_LESS,$KSSJ_START,$JSSJ_END,$offset,$rows,$sort,$order);
		echo json_encode($result);
	}
	
	
}
?>
